package com.qmx.qmxapp.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.google.gson.JsonObject;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.qmx.qmxapp.MainActivity;
import com.qmx.qmxapp.R;
import com.qmx.qmxapp.base.BaseActivity;
import com.qmx.qmxapp.bean.LzyResponse;
import com.qmx.qmxapp.bean.TestModel;
import com.qmx.qmxapp.callback.JsonCallback;
import com.qmx.qmxapp.constant.AppConstant;
import com.qmx.qmxapp.constant.ComParamContact;
import com.qmx.qmxapp.token.TokenManager;
import com.qmx.qmxapp.utils.SocketUtil;

import org.json.JSONArray;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author chengxiangtong
 * @date 2018/10/10
 */
public class ChooseLoginWaysActivity extends BaseActivity {
    @BindView(R.id.join_meeting)
    Button join_meeting;
    @BindView(R.id.login)
    Button login;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_login_ways);
        ButterKnife.bind(this);
        // 读取配置文件
        loadConfigData();

        // 申请动态权限
        checkPermission();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
        //        TokenManager.getInstance().clearAuth();
        if (TokenManager.getInstance().isLogin()) {
            SocketUtil.getInstance();

            afterUserSelected(sharedPreferences.getString(AppConstant.USER_ID, ""));

            getHttpToken();
            Log.e("ChooseLoginWaysActivity", TokenManager.getInstance().getAuthModel().getToken());
            //            afterUserSelected(sharedPreferences.getString(AppConstant.USER_ID, ""));
            startActivity(new Intent(this, MainActivity.class));
            finish();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    //多个控件可以一起发在里面进行监听
    @OnClick({R.id.join_meeting, R.id.login, R.id.app_register})
    public void onclick(View view) {
        Intent i = new Intent();
        switch (view.getId()) {
            case R.id.join_meeting:
                i.setClass(this, JoinMeetingSetActivity.class);
                break;
            case R.id.app_register:
                i.setClass(this, RegisterAccountActivity.class);
                break;
            case R.id.login:
                i.setClass(this, LoginActivity.class);
                //                getDepList();
                //                return;
                break;
            default:
                break;
        }
        startActivity(i);
    }

    public void getDepList() {
        //        OkGo.<String>post(AppConstant.Url + ComParamContact.contactList.PATH)
        //                .headers(ComParamContact.tokenCheck.TOKEN, TokenManager.getInstance().getAuthModel().getToken())
        //                .execute(new StringCallback() {
        //                    @Override
        //                    public void onSuccess(Response<String> response) {
        ////                        Gson gson = new Gson();
        ////                        ContactModel d = gson.fromJson(response.getRawResponse().body()., ContactModel.class);//取值的时候就从父类一层一层调子类成员（重要）
        //                        Log.e("onSuccess", response.body().toString());
        //                    }
        //
        //                    @Override
        //                    public void onError(Response<String> response) {
        //                        Log.e("onError", response.toString());
        ////                            handleError(response);
        //                    }
        //                });
        OkGo.<LzyResponse<TestModel>>post(AppConstant.Url + ComParamContact.contactList.PATH)
                .headers(ComParamContact.tokenCheck.TOKEN, TokenManager.getInstance().getAuthModel().getToken())
                .execute(new JsonCallback<LzyResponse<TestModel>>() {
                    @Override
                    public void onSuccess(Response<LzyResponse<TestModel>> response) {
                        Log.e("onSuccess", response.body().data.getData().toString());
                    }

                    @Override
                    public void onError(Response<LzyResponse<TestModel>> response) {
                        Log.e("onError", response.toString());
                        //                            handleError(response);
                    }
                });
    }
}
